var layer;
var $;
var element;
var form;
var laydate;
var table;
var upload;
layui.use(['element','layer','laydate','form','table','upload'], function(){
    layer = layui.layer;
    $ = layui.jquery;
    element = layui.element;
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    upload = layui.upload;
    laydate.render({
        elem: '#birthDate', //指定元素
        type: 'datetime',
        trigger: 'click' //时间时间为点击事件
    });
    load();

})
function load(){
    var avatarUrl = $("input[name=avatarUrl]").val();
    $(".img_title").attr("src",avatarUrl);
    //点击上传图片
    $("#updimga").on("click",function(){
        $("#imgbut").click();
    });
    //图片上传
    var uploadInst = upload.render({
        elem: '#imgbut',
        url: '/personnel/updBackUserImg',//改成您自己的上传接口,
        accept:'images',
        size: 5000, //限制文件大小单位 KB
        before: function (obj) {
            var userId = $("input[name=userId]").val();
            this.data={'userId':userId}
            //预读本地文件示例
            obj.preview(function (index, file, result) {
                $('.img_title').attr('src', result); //图片链接(base64)
            });
        }, done: function (res) {
            layer.msg('更换成功', {icon: 1});
            parent.selIndexUserTab();
            //上传成功的一些操作
        }, error: function () {
            //失败
            layer.msg('更换失败', {icon: 2});
        }
    });
}
/**
 *  点击编辑部门
 * */
function updDept(){
    layer.open({
        type:2,//弹出层的类型
        title:"编辑部门信息",
        area:['565px','355px'],//弹出层大小
        shade:0,//设置遮罩
        offset:'auto',//设置遮罩的位置
        //弹出层内容
        content : "/personnel/layer/PerBackstageUser_layer_upd_Dept.html",
        success:function(layero,index){
        }
    })
}
/**
*  点击编译  开始编译
* */
function updThinkUserInfo(){
    layer.msg("开始编译", {icon: 1});
    $(".account_input").removeAttr("disabled");
    $(".trueName_input").removeAttr("disabled");
    $(".phoneNumber_input").removeAttr("disabled");
    $(".idCard_input").removeAttr("disabled");
    $("#birthDate").removeAttr("disabled");
    $(".deptName_input").removeAttr("disabled");
    $(".freestyle_textarea").removeAttr("disabled");
    $(".email_input").removeAttr("disabled");
    $(".div_dept_li").attr("style","width: 93%;");
    $(".dep_icon").attr("style","display:block");
    $(".dprt_icon").attr("style","display:block");
    $("#select_id").attr("style","display:block");
    $("#select_id2").attr("style","display:none");
    $("#div_p_uplo").attr("style","display:block");
    $(".upd_btn").removeAttr("onclick");
    $(".upd_btn").attr("onclick","submitUpdInfo()");
    $(".upd_btn").val("提交");
}
function submitUpdInfo(){
    var form = '';
    var userId = $("input[name=userId]").val();
    var account = $("input[name=account]").val();
    var trueName = $("input[name=trueName]").val();
    var sex = $("select[name=sex]").val();
    var phoneNumber = $("input[name=phoneNumber]").val();
    var idCard = $("input[name=idCard]").val();
    var birthDate = $("input[name=birthDate]").val();
    var deptName = $("input[name=deptName]").val();
    var deptId = $("input[name=deptId]").val();
    var email = $("input[name=email]").val();
    var freestyle = $("textarea[name=freestyle]").val();
    if(!account){
        form+="&account="+'';
    }else{
        form+="&account="+account;
    }
    if(!trueName){
        form+="&trueName="+'';
    }else{
        form+="&trueName="+trueName;
    }
    if(!sex){
        form+="&sex="+'';
    }else{
        form+="&sex="+sex;
    }
    if(!phoneNumber){
        form+="&phoneNumber="+0;
    }else{
        form+="&phoneNumber="+phoneNumber;
    }
    if(!idCard){
        form+="&idCard="+'';
    }else{
        form+="&idCard="+idCard;
    }
    if(!birthDate){
        form+="&birthDate="+'';
    }else{
        form+="&birthDate="+birthDate;
    }
    if(!deptName){
        form+="&deptName="+'';
    }else{
        form+="&deptName="+deptName;
    }
    if(!deptId){
        form+="&deptId="+0;
    }else{
        form+="&deptId="+deptId;
    }
    if(!email){
        form+="&email="+'';
    }else{
        form+="&email="+email;
    }
    if(!freestyle){
        form+="&freestyle="+'';
    }else{
        form+="&freestyle="+freestyle;
    }
    if(!userId){
        form+="&userId="+0;
    }else{
        form+="&userId="+userId;
    }
    $.ajax({
        url:"/personnel/updBackUserInfo",
        type:"post",
        dataType:"json",
        data:form,
        success:function(item) {
            var tablename = "后台管理员数据列表";
            var type=4;
            var remark = userId+"";
            addOperLog(tablename,type,remark);
            if(item){
                window.parent.selIndexUserTab();
                var layer1 = parent.layer;
                parent.layer.close(layer1.index);
                parent.layer.msg("编译成功", {icon: 1});
            }else{
                layer.msg("编译失败", {icon: 2});
            }
        }
    })
    $(".upd_btn").removeAttr("onclick");
    $(".upd_btn").attr("onclick","updThinkUserInfo()");
    $(".upd_btn").val("编译");
    $(".account_input").attr("disabled","disabled");
    $(".trueName_input").attr("disabled","disabled");
    $(".phoneNumber_input").attr("disabled","disabled");
    $(".idCard_input").attr("disabled","disabled");
    $("#birthDate").attr("disabled","disabled");
    $(".deptName_input").attr("disabled","disabled");
    $(".freestyle_textarea").attr("disabled","disabled");
    $("#select_id").attr("style","display:none");
    $("#select_id2").attr("style","display:block");
    $(".div_dept_li").attr("style","width: 100%;");
    $(".dep_icon").attr("style","display:none");
    $(".dprt_icon").attr("style","display:none");
    $("#div_p_uplo").attr("style","display:none");
    $(".email_input").attr("disabled","disabled");
    $(".freestyle_textarea").attr("disabled","disabled");
}
/**
 *  点击 取消关闭弹出层
 */
function closeLayer(){
    var layer=parent.layer;
    layer.close(layer.index);
}
/**
 *  添加后台操作日志
 * */
function addOperLog(tablename,type,remark){
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    var mid = create.userId;
    form+="&mid="+mid;
    form+="&tablename="+tablename;
    form+="&type="+type;
    form+="&remark="+remark;
    $.post("/personnel/addOperationLog", form, function (res) {
        form="";
    });
}
